import Vue from 'vue'
import Vuex from 'vuex'
import Request from '@/common/request.js'
import permision from "@/js_sdk/wa-permission/permission.js";

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		location: {
			latitude: "26",
			longitude: "119"
		},
		city: '福州市',
		isRealName: false
	},
	mutations: {
		setLocation(state, location) {
			state.location = location
		},
		setCity(state, city) {
			state.city = city
		},
		setIsRealName(state, isRealName) {
			state.isRealName = isRealName
		},
		setaddstatus(state, status) {
			state.addstatus = status
		}
	},
	actions: {
		async getpermission({
			commit,
			state
		}) {
			// 统一获取权限
			if (state.addstatus == '') {
				const permissionDenied = uni.getStorageSync('ACCESS_FINE_LOCATION');
				if (permissionDenied) {
					console.log('用户已拒绝过权限请求，不再重复请求');
				} else {
					let getaddstatus = await permision.requestAndroidPermission(
						'android.permission.ACCESS_FINE_LOCATION')
					if (getaddstatus == 1) {
						commit('setaddstatus', getaddstatus);
					} else if (getaddstatus == 0) {
						uni.setStorageSync('locationDenied', true);
					} else {
						uni.setStorageSync('locationDenied', true);
					}
				}
			}
		},
		async getLocation({
			commit
		}) {
			try {
				// #ifdef APP
				const locationDenied = uni.getStorageSync('ACCESS_FINE_LOCATION');
				if (locationDenied) {
					console.log('用户已拒绝过定位权限，不再请求');
					return; // 直接退出，避免重复请求
				}
				let getaddstatus = await permision.requestAndroidPermission(
					'android.permission.ACCESS_FINE_LOCATION')
				if (getaddstatus == 1) {
					// 尝试请求定位
					const res = await new Promise((resolve, reject) => {
						uni.getLocation({
							success: resolve,
							fail: reject
						});
					});
					// 成功获取定位
					const location = {
						latitude: res.latitude.toString(),
						longitude: res.longitude.toString()
					};
					commit('setLocation', location);

					const params = {
						lat: res.latitude.toString(),
						lng: res.longitude.toString()
					};

					const cityInfoRes = await Request({
						method: 'POST',
						url: '/api/index/getCityInfo',
						data: params,
						ismsg: false,
					});
					
					if (cityInfoRes.code == 1) {
						console.log(cityInfoRes.data.province,1111111111111111111)
						// #ifdef MP-WEIXIN
						commit('setCity', cityInfoRes.data.city.length ? cityInfoRes.data.city : cityInfoRes
							.data.province);
						// #endif
						// #ifdef APP
						commit('setCity', cityInfoRes.data.province);
						// #endif
					}
				} else if (getaddstatus == 0) {
					uni.setStorageSync('ACCESS_FINE_LOCATION', true);
				} else {
					uni.setStorageSync('ACCESS_FINE_LOCATION', true);
				}
				// #endif
				// #ifdef MP-WEIXIN  || H5
				// 尝试请求定位
				const res = await new Promise((resolve, reject) => {
					uni.getLocation({
						success: resolve,
						fail: reject
					});
				});
				// 成功获取定位
				const location = {
					latitude: res.latitude.toString(),
					longitude: res.longitude.toString()
				};
				commit('setLocation', location);

				const params = {
					lat: res.latitude.toString(),
					lng: res.longitude.toString()
				};

				const cityInfoRes = await Request({
					method: 'POST',
					url: '/api/index/getCityInfo',
					data: params,
					ismsg: false,
				});

				if (cityInfoRes.code === 1) {
					commit('setCity', cityInfoRes.data.city.length ? cityInfoRes.data.city : cityInfoRes
						.data.province);
				}
				// #endif
			} catch (err) {
				console.error('获取定位失败：', err);
				uni.setStorageSync('ACCESS_FINE_LOCATION', true);
			}
		},
		async checkRealName({
			commit
		}) {
			const res = await Request({
				method: 'POST',
				url: '/api/user/getMyInfo',
				ismsg: false,
			});
			if (res.code === 1) {
				commit('setIsRealName', res.data.face_status !== 0);
			}
		}
	},
})

export default store